package edu.yctc.patterns.controller.impl;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.yctc.patterns.constant.ErrorText;
import edu.yctc.patterns.constant.StaticPath;
import edu.yctc.patterns.constant.TemplatePath;
import edu.yctc.patterns.controller.UserController;
import edu.yctc.patterns.controller.session.SessionContentHolder;
import edu.yctc.patterns.entity.ResultDO;
import edu.yctc.patterns.entity.UserDO;
import edu.yctc.patterns.service.UserIService;

/**
 * userController实现类
 * 
 * @author 赵宜珺
 */
@Controller
@ComponentScan({"edu.yctc.patterns.service"})
@RequestMapping("/user")
public class UserControllerImpl implements UserController {

    private final static Logger LOG = LoggerFactory.getLogger("controllerLogger");

    @Autowired
    private UserIService userService;

    @Override
    @GetMapping("signin")
    public String showSignin() {
        return TemplatePath.USER_SIGNIN;
    }

    @Override
    @PostMapping("signin")
    public String signin(@ModelAttribute UserDO userDO, HttpSession httpSession) {
        // 参数检验
        if (StringUtils.isBlank(userDO.getAccount()) || StringUtils.isBlank(userDO.getPassword())) {
            LOG.error("signin fail, parameter invalid, userDO={}", userDO);
            return "redirect:/" + StaticPath.COMMON_ERROR + "?" + ErrorText.PARAMETER_INVALID;
        }
        try {
            ResultDO<Long> resultDO = userService.signin(userDO);
            if (resultDO.isSuccess() == false) {
                LOG.error("sigin fail, userDO={}, resultDO={}", userDO, resultDO);
                return "redirect:/" + StaticPath.COMMON_ERROR + "?" + resultDO.getMsg();
            }
            long userId = resultDO.getModule();
            if (userId <= 0) {
                LOG.error("sign in fail, userDO={}, sessionId={}, resultDO={}", userDO, httpSession.getId(), resultDO);
                return "redirect:/" + StaticPath.COMMON_ERROR + "?" + resultDO.getMsg();
            }
            // 登陆成功
            SessionContentHolder.addSignInUserId(httpSession.getId(), resultDO.getModule());
            LOG.info("sign in success, userDO={}, sessionId={}", userDO, httpSession.getId());
            return TemplatePath.USER_HOME;
        } catch (Exception e) {
            LOG.error("signin error, userDO={}", userDO, e);
            return "redirect:/" + StaticPath.COMMON_ERROR + "?" + ErrorText.SYSTEM_ERROR;
        }
    }

}
